/**
 * 功能描述�?
 */

define(function (require, exports, module) {
	var Component = require("grid");
	var Dialog=require("dialog");
	var aw = require("ajaxwrapper");
	var fetchProperties = require("../constant/fetchProperties")
	
    var instance = {
        initComponent : function (params, widget) {
            var component = new Component({
            	parentNode : ".J-grid",	
            	url : "api/product/query",
            	params : function(){
					var subnav = widget.get("subnav");
					var isSales=subnav.getValue("isSales");
					if(isSales=="All"){
						var obj = {
								fetchProperties:"*,organization.*,communityDatas.*"
							};
					}else{
						var obj = {
								"sales":isSales,
								fetchProperties:"*,organization.*,communityDatas.*"
							};
					}
	    			return obj;
				},
				model : {
				 columns  : [{
					 name : "name",
					 label : "名称",
				 },{
					 name : "isReturn",
					 label : "可退还",
					 format:function(value,row){
						if(value){
							return "是";
						}else{
							return "否";
						}
                     },
				 },{
					 name : "transfer",
					 label : "可转让",
					 format:function(value,row){
						if(value){
							return "是";
						}else{
							return "否";
						}
                     },
				 },{
					 name : "isExtends",
					 label : "可继承",
					 format:function(value,row){
						if(value){
							return "是";
						}else{
							return "否";
						}
                     },
				 },{
					 name : "lendable",
					 label : "可借出",
					 format:function(value,row){
						if(value){
							return "是";
						}else{
							return "否";
						}
                     },
				 },{
					 name : "renewable",
					 label : "可续签",
					 format:function(value,row){
						if(value){
							return "是";
						}else{
							return "否";
						}
                     },
				 },{
					 name : "sales",
					 label : "促销状态",
					 format:function(value,row){
						if(value){
							return "打开";
						}else{
							return "关闭";
						}
                     },
				 },{
					 name : "operate",
					 label : "操作",
					 format:"button",
						formatparams:[{
							id:"open",
							text:"打开",
							show:function(value,row){
								if(!row.sales){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								if(!data.sales){
									aw.ajax({
										url: "api/productrule/updata",
										data:{
											pkProductRuleDetail:data.pkProductRuleDetail,
											version:data.version
										},
										dataType:"json",
										success:function(data){
											widget.get("grid").refresh();
										}
									});
								}else{
									Dialog.alert({
				                		title:"打开失败",
				                		content:"该状态下不能再进行打开操作！"
				                	});
				            		return false;
								}
							}
						},{
							id:"close",
							text:"关闭",
							show:function(value,row){
								if(row.sales){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								if(data.sales){
									aw.ajax({
										url: "api/productrule/updata",
										data:{
											pkProductRuleDetail:data.pkProductRuleDetail,
											version:data.version
										},
										dataType:"json",
										success:function(data){
											widget.get("grid").refresh();
										}
									});
								}else{
									Dialog.alert({
				                		title:"关闭失败",
				                		content:"该状态下不能再进行关闭操作！"
				                	});
				            		return false;
								}
							}
						},{
							key:"edit",
							icon:"icon-edit",
							show:function(value,row){
								if(row.sales){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								widget.get("form").setData(data);
								var temp=data.communityDatas;
								var community=[];
								for(var i in temp){
									community.push(temp[i].pkCommunityData);
								}
								widget.get("form").setValue("communityDatas",community);
								aw.ajax({
									url: "api/productdetail/queryall",
									data:{
										"pkProductRuleDetail":data.pkProductRuleDetail,
										fetchProperties:"*,productRule.*,productRule.product.*,productBatch.*,productType.*,expenseScope.*,bedScope.*,cashbackScope.*,subsidyScope.*"
									},
									dataType:"json",
									success:function(datas){
										widget.get("editgrid").setData(datas);
										widget.get("form").setValue("organization",data.organization.pkOrganization);
										widget.hide([".J-grid"]).show([".J-form",".J-editgrid"]);
										$(".J-button").removeClass("hidden")
										widget.get("subnav").hide(["isSales","search","add"]).show(["cancel","save"]);
									}
								});
							}
						},{
							key:"delete",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								aw.del("api/product/"+data.pkProduct+"/deleteall",function() {
									widget.get("grid").refresh();
								});	
							}
						}]
				 }]
				}
            });
            widget.set("grid", component);
        }
    };
    module.exports = instance;
});
